package com.pks.datacenter.controller.basic;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

//import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.pks.datacenter.entity.basic.OperLog;
import com.pks.datacenter.service.basic.OperLogService;
import com.pks.resultful.common.SRemoteResult;
//import com.pks.util.UtilConvert;
import com.pks.util.UtilString;
import com.pks.web.springmvc.controller.BasicController;


@RestController
@RequestMapping(value = "/operLog")
@Scope("prototype")
@Component("operLogController")
@Api(tags = "操作日志")
public class OperLogController extends BasicController {

	@Resource
	private OperLogService operLogService;

//	@ApiOperation(value = "删除记录")
//	@RequestMapping(value = "/delete" , method = RequestMethod.POST)
//	public SRemoteResult<String> delete(HttpServletRequest request, String ids) {
//		if (UtilString.isBlank(ids)) {
//			return SRemoteResult.initErrorParamBlank(null);
//		}
//		if (StringUtils.isBlank(ids) || !ids.matches("^[0-9]+(,[0-9]+)*$")) {
//			return SRemoteResult.initErrorParamInvalid(null);
//		}
//		List<Long> idList = UtilConvert.string2LongList(ids);
//		if (idList.size() == 1) {
//			operLogService.delete(idList.get(0));
//		} else {
//			operLogService.deleteByIds(idList);
//		}
//		return SRemoteResult.initSuccess(null);
//	}
//	
//	@ApiOperation(value = "新增或修改")
//	@RequestMapping(value = "/modify" , method = RequestMethod.POST)
//	public SRemoteResult<String> modify(HttpServletRequest request, OperLog obj) {
//		if (obj == null) {
//			return SRemoteResult.initErrorParamMissing(null);
//		}
//		if (obj.getId() == null) {
//			obj.insertBefore();
//			operLogService.insert(obj);
//		} else {
//			OperLog update = operLogService.findById(obj.getId());
//			if (update == null) {
//				return SRemoteResult.initErrorDataNone();
//			}
//			operLogService.update(obj);
//		}
//		return SRemoteResult.initSuccess(null);
//	}
//
//	@ApiOperation(value = "获取详情")
//	@ApiImplicitParams({
//		@ApiImplicitParam(name = "id",value = "id", dataType = "long" , paramType = "query" )
//	})
//	@RequestMapping(value = "/get" , method = RequestMethod.GET)
//	public SRemoteResult<OperLog> findInfo(HttpServletRequest request, Long id) {
//		if (id == null) {
//			return SRemoteResult.initErrorParamMissing(null);
//		}
//		OperLog obj = operLogService.findById(id);
//		if (obj == null) {
//			return SRemoteResult.initErrorDataNone();
//		}
//		return SRemoteResult.initSuccess(obj);
//	}

	@ApiOperation(value = "列表查询")
	@ApiImplicitParams({
		@ApiImplicitParam(name = "userName",value = "操作人姓名", dataType = "String" , paramType = "query" )
		,@ApiImplicitParam(name = "operModule",value = "操作模块", dataType = "String" , paramType = "query" )
		,@ApiImplicitParam(name = "operType",value = "操作类型", dataType = "String" , paramType = "query" )
		,@ApiImplicitParam(name = "operType",value = "操作类型", dataType = "String" , paramType = "query" )
		,@ApiImplicitParam(name = "offset",value = "分页起始页（默认0）", dataType = "int" , paramType = "query" )
		,@ApiImplicitParam(name = "pagesize",value = "分页每页行数（默认10）", dataType = "int" , paramType = "query" )
	})
	@RequestMapping(value = "/list" , method = RequestMethod.POST)
	public SRemoteResult<List<OperLog>> list(HttpServletRequest request ,String userNameLike  ,String operModuleLike  ,String operTypeLike ,String operType ,Integer offset,Integer pagesize) {
		Map<String, Object> qbuilder = new HashMap<String, Object>();
		if (UtilString.isNotBlank(userNameLike)) {
			qbuilder.put("userNameLike", "%" + userNameLike + "%");
		}
		if (UtilString.isNotBlank(operModuleLike)) {
			qbuilder.put("operModuleLike", "%" + operModuleLike + "%");
		}
		if (UtilString.isNotBlank(operType)) {
			qbuilder.put("operTypeLike", "%" + operType + "%");
		}
		if (UtilString.isNotBlank(operType)) {
			qbuilder.put("operType", operType);
		}
		List<OperLog> list = new ArrayList<OperLog>();
		int total = 0;
		total = operLogService.getTotalRows(qbuilder);
		if (total > 0) {
			list = operLogService.findByPage(null, qbuilder);
		}
		return SRemoteResult.initPageList(list, total);
	}


}
